package store.aixx.jwt.demo.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
import store.aixx.jwt.demo.util.MessageUtil;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author yukai
 * @since 2021年12月11日 1:33
 */
@Builder
@Data
public class R<T> implements Serializable {
    private String code;

    private String message;

    private T data;

    private Boolean success;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime timestamp;

    public static<T> R<T> Success(){
        return R.<T>builder()
                .code("00000")
                .success(true)
                .message(MessageUtil.getMessage("operation.success"))
                .timestamp(LocalDateTime.now())
                .build()
                ;

    }

    public static<T> R<T> Success(T data){
        return R.<T>builder()
                .code("00000")
                .success(true)
                .message(MessageUtil.getMessage("operation.success"))
                .timestamp(LocalDateTime.now())
                .data(data)
                .build()
                ;

    }

    public static<T> R<T> Success(String message, T data, Object...i18nArgs){
        return R.<T>builder()
                .code("00000")
                .success(true)
                .message(MessageUtil.getMessage(message, i18nArgs))
                .timestamp(LocalDateTime.now())
                .data(data)
                .build()
                ;

    }

    public static R Success(String message,  Object...i18nArgs){
        return R.builder()
                .code("00000")
                .success(true)
                .message(MessageUtil.getMessage(message, i18nArgs))
                .timestamp(LocalDateTime.now())
                .build()
                ;
    }

    public static R error(IErrorStatus status, Object...i18nArgs){
        return R.builder()
                .code(status.getCode())
                .message(MessageUtil.getMessage(status.getMessage(), i18nArgs))
                .success(false)
                .timestamp(LocalDateTime.now())
                .build();
    }



}
